﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QLDAOL.Models;
using QLDAOL.ModelView;
using DHTMLX.Scheduler;
using DHTMLX.Scheduler.Data;
using DHTMLX.Common;
namespace QLDAOL.Controllers
{
    public class CalendarController : BaseController
    {
        //
        // GET: /Calendar/
        private IEQLDAOLRepository db = new IEQLDAOLRepository();
        public ActionResult Index()
        {
            var scheduler = new DHXScheduler(this); //initializes dhtmlxScheduler
            scheduler.LoadData = true;// allows loading data
            scheduler.EnableDataprocessor = true;// enables DataProcessor in order to enable implementation CRUD operations
    
            return View(scheduler);
           
        }
        public ActionResult Index2()
        {
            var scheduler = new DHXScheduler(this); //initializes dhtmlxScheduler
            scheduler.LoadData = true;// allows loading data
            scheduler.EnableDataprocessor = true;// enables DataProcessor in order to enable implementation CRUD operations
            scheduler.Skin = DHXScheduler.Skins.Standart;
            return View(scheduler);

        }
        public ActionResult Data()
        {//events for loading to scheduler
            int id =(int) Session["pjID"];
            var t= db.getAllPJCalendar(id);
            List<CalendarModel> c =new List<CalendarModel>();
            foreach (var a in t)
            {
                CalendarModel n = new CalendarModel
                {
                    id=a.ID,
                    text=a.Description,
                    end_date=a.End_date,
                    start_date=a.Start_date
                };
                c.Add(n);
                
            }
            return new SchedulerAjaxData(c);
        }
        public ActionResult Save(PJ_CALENDAR updatedEvent, FormCollection formData)
        {
            var action = new DataAction(formData);
            updatedEvent.Pj_id =(int) Session["pjID"];
            updatedEvent.Description = formData["text"].ToString();

            try
            {
                switch (action.Type)
                {
                    case DataActionTypes.Insert: // your Insert logic
                        db.insertPjCalendar(updatedEvent);
                       // add vào từng user
                        db.inserttopjuser(updatedEvent.Pj_id, updatedEvent);
                       break;
                    case DataActionTypes.Delete: // your Delete logic
                        updatedEvent = db.getPjCalendarByID(updatedEvent.ID);
                        db.deletePjCalendar(updatedEvent);
                        db.deletetopjuser(updatedEvent.Pj_id, updatedEvent);
                        break;
                    default:// "update"   // your Update logic
                       // updatedEvent = db.getPjCalendarByID(updatedEvent.ID);
                        db.UpdatePjCalendar(updatedEvent);
                        db.updatetopjuser(updatedEvent.Pj_id, updatedEvent);
                        break;
                }
              
                action.TargetId = updatedEvent.ID;
            }
            catch (Exception a)
            {
                action.Type = DataActionTypes.Error;
            }
            return (new AjaxSaveResponse(action));
        }

    }
}
